package com.animee.tally.main_frag;
import android.inputmethodservice.KeyboardView;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.animee.tally.R;
import com.animee.tally.db.AccountBean;
import com.animee.tally.db.TypeBean;
import com.animee.tally.util.BeizhuDialog;
import com.animee.tally.util.KeyBoardUtil;
import com.animee.tally.util.SelectTimeDialog;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/* 将收入和支出的代码公共部分提取出来*/
abstract public class BaseFragment extends Fragment implements View.OnClickListener {
    KeyboardView keyboardView;
    EditText moneyEt;
    ImageView typeIv;
    TextView typeTv,beizhuTv,timeTv;
    GridView typeGv;
    //    创建GridView对应的数据源
    List<TypeBean> typeList;
    TypeBaseAdapter adapter;

    AccountBean accountBean;         //将要插入的数据以对象的形式进行声明

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        accountBean = new AccountBean();   // 创建对象
        accountBean.setTypename("其他");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.fragment_outcome, container, false);
        initView(view);
        setInitTime();
        loadDataToGV();
        setGVListener();
        setClickListener();
        return view;
    }
//   给备注和时间设置点击事件
    private void setClickListener() {
        beizhuTv.setOnClickListener(this);
        timeTv.setOnClickListener(this);
    }
    /* 给gridview的每一项添加监听事件*/
    private void setGVListener() {
        typeGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.selectPos = position;
                adapter.notifyDataSetChanged();
//                改变上面的图片展示和文字
                TypeBean typeBean = typeList.get(position);
                String typename = typeBean.getTypename();
                accountBean.setTypename(typename);
                typeTv.setText(typename);
                int sImageId = typeBean.getSImageId();
                accountBean.setsImageId(sImageId);
                typeIv.setImageResource(sImageId);
            }
        });
    }
    // 给gradview填充数据的方法
    private void loadDataToGV() {
        typeList = new ArrayList<>();
        adapter = new TypeBaseAdapter(getContext(), typeList);
        typeGv.setAdapter(adapter);
    }
    /* 查找fragment当中控件的方法*/
    private void initView(View view) {
        keyboardView = view.findViewById(R.id.frag_out_keyboard);
        moneyEt = view.findViewById(R.id.frag_out_et_money);

        KeyBoardUtil boardUtil = new KeyBoardUtil(keyboardView, moneyEt);
        boardUtil.showKeyboard();   //弹出自定义的软键盘
        typeIv = view.findViewById(R.id.frag_out_iv);
        typeTv = view.findViewById(R.id.frag_out_tv_type);
        beizhuTv = view.findViewById(R.id.frag_out_tv_beizhu);
        timeTv = view.findViewById(R.id.frag_out_tv_time);
        typeGv = view.findViewById(R.id.frag_out_gv);

        //  点击确定时会调用的方法
         boardUtil.setOnEnsureListener(new KeyBoardUtil.OnEnsureListener() {
             @Override
             public void onEnsure() {
                 String monstr = moneyEt.getText().toString();
                 if(TextUtils.isEmpty(monstr) || monstr.equals("0")){
                     getActivity().finish();
                     return;
                 }
                  float money = Float.parseFloat(monstr);
                 accountBean.setMoney(money);
                  saveAccount();
                 getActivity().finish();
             }
         });
    }

    private void setInitTime() {
        //获取当前时间，显示在timeTv上
        Date date = new Date();   //获取当前日期对象
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        String format = sdf.format(date);
        timeTv.setText(format);
        accountBean.setTime(format);
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH)+1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        accountBean.setYear(year);
        accountBean.setMonth(month);
        accountBean.setDay(day);
    }

    /* 为了能够继承的fragment重写这个方法*/
    public abstract void saveAccount();

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.frag_out_tv_beizhu:
                showBZDialog();
                break;
            case R.id.frag_out_tv_time:
                showTimeDialog();
                break;
        }
    }

    private void showTimeDialog() {
        //弹出显示时间的对话框
        SelectTimeDialog dialog = new SelectTimeDialog(getActivity());
        dialog.show();
        dialog.setOnEnsureListener(new SelectTimeDialog.OnEnsureListener() {
            @Override
            public void onEnsure(String time1, int year1, int month1, int day1) {
                timeTv.setText(time1);
                accountBean.setTime(time1);
                accountBean.setYear(year1);
                accountBean.setMonth(month1);
                accountBean.setDay(day1);
            }
        });
    }

    private void showBZDialog() {
        //  点击添加备注，弹出对话框
        final BeizhuDialog dialog = new BeizhuDialog(getActivity());
        dialog.show();
        dialog.setDialogSize();
        dialog.setOnEnsureListener(new BeizhuDialog.OnEnsureListener() {  //点击了对话框中的确定按钮
            @Override
            public void onEnsure() {
                String editText = dialog.getEditText();
                if (!TextUtils.isEmpty(editText)) {
                    beizhuTv.setText(editText);
                    accountBean.setBeizhu(editText);
                }
                dialog.cancel();
            }
        });
    }
}
